package org.mybatis.generator.api.plus;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.io.*;
import java.util.Iterator;
import java.util.List;

/**
 * @author wangfupeng
 */
public class MybatisUtilDistinctService {

    public static void writeDistinctService(String basePackageName, String aspectPackageName, String targetProject, TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String tableName, String servicePackage) {
        File file = null;
        OutputStream fileOutputStream = null;
        BufferedWriter bufferedWriter = null;
        try {
            String packageNameFolder = servicePackage.replaceAll("\\.", "/");
            targetProject = targetProject.replaceAll("\\./", "");
            File folder = new File(targetProject + "/" + packageNameFolder + "/distinct");
            folder.mkdirs();
            file = new File(targetProject + "/" + packageNameFolder + "/distinct/Distinct" + tableName + "Service.java");
            //如果文件不存在，则创建文件
            file.createNewFile();
            fileOutputStream = new FileOutputStream(file);
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, "utf-8"));
            bufferedWriter.write("package " + servicePackage + ".distinct;\n");
            bufferedWriter.write("" +
                    "import " + basePackageName + ".bean.mapper.distinct.Distinct" + tableName + "Mapper;\n" +
                    "import " + aspectPackageName + ".CacheMockThenFetchLater;\n" +
                    "import org.springframework.beans.factory.annotation.Autowired;\n" +
                    "import org.springframework.stereotype.Service;\n"+
                    "\n" +
                    "import java.util.List;\n" +
                    "\n" +
                    "@Service\n" +
                    "public class Distinct" + tableName + "Service {\n" +
                    "    \n" +
                    "    @Autowired\n" +
                    "    Distinct" + tableName + "Mapper distinct" + tableName + "Mapper;\n" +
                    "    \n");

            /*List<IntrospectedColumn> introspectedColumns = introspectedTable.getPrimaryKeyColumns();*/
            List<IntrospectedColumn> introspectedColumns = introspectedTable.getAllColumns();
            Iterator<IntrospectedColumn> iterator = introspectedColumns.iterator();
            while (iterator.hasNext()) {
                IntrospectedColumn introspectedColumn = iterator.next();
                String columnName = introspectedColumn.getJavaProperty();
                bufferedWriter.write("" +
                        "    public List getDistinct" + MybatisUtilCommon.getProperCase(columnName) + "(){\n" +
                        "        return distinct" + tableName + "Mapper.getDistinct" + MybatisUtilCommon.getProperCase(columnName) + "();\n" +
                        "    }\n" +
                        "\n");
            }
            bufferedWriter.write("" +
                    "    @CacheMockThenFetchLater\n" +
                    "    public List getDistinct(String field){\n");
            List<IntrospectedColumn> introspectedColumns2 = introspectedTable.getAllColumns();
            Iterator<IntrospectedColumn> iterator2 = introspectedColumns2.iterator();
            while (iterator2.hasNext()) {
                IntrospectedColumn introspectedColumn = iterator2.next();
                String columnName = introspectedColumn.getJavaProperty();
                bufferedWriter.write("        if(field.equals(\""+MybatisUtilCommon.toLowerCaseAtFirstCharacter(columnName)+"\")){\n" +
                        "            return getDistinct" + MybatisUtilCommon.getProperCase(columnName) + "();\n");
                bufferedWriter.write("        }\n");
            }
            bufferedWriter.write("        return null;\n    }\n");
            bufferedWriter.write("}");
            bufferedWriter.write("\n");
            System.out.println("write to file success : " + file.getPath());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedWriter != null) {
                    bufferedWriter.flush();
                    bufferedWriter.close();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
